#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os
import xml.dom.minidom
from Scripts.Startup import StartupConfigData

#
# load startup config data strcuture
# @param [startupFileUrl] startup file url
# @return output folder urls
#
def Load(startupFileUrl):
    #get xml document object
    xmlDocument = _DoLoadXml(startupFileUrl)
    if None == xmlDocument:
        return None

    #xml root
    xmlRoot = xmlDocument.documentElement

    #gets output folder urls
    outputType, outputFolderUrls = _DoReadOutput(xmlRoot)
    if None == outputType or None == outputFolderUrls:
        return None, None, None

    #gets config datas
    configDatas = _DoReadData(xmlRoot)
    if None == configDatas:
        return None, None, None

    return outputType, outputFolderUrls, configDatas


def _DoLoadXml(startupFileUrl):
    xmlDocument = None
    try:
        xmlDocument = xml.dom.minidom.parse(startupFileUrl)
    except Exception as err:
        xmlDocument = None
        print(err)
    return xmlDocument


def _DoReadOutput(xmlRoot):
    if None == xmlRoot:
        return None, None

    #gets xml output root
    outputRoots = xmlRoot.getElementsByTagName("Output")
    if len(outputRoots) == 0:
        print("[Error] there has no output url root")
        return None, None

    #gets config type
    outputRoot = outputRoots[0]
    outputType = outputRoot.getAttribute("Type")

    #gets output urls
    outputFolderUrls = []
    outputUrls = outputRoot.getElementsByTagName("Url")
    if len(outputUrls) == 0:
        print("[Error] there has no output url")
        return None, None

    #gets current folder url
    currentFolderUrl = os.getcwd()

    #gets all of the output urls
    for i in range(len(outputUrls)):
        outputUrlElement = outputUrls[i]
        outputUrl = outputUrlElement.getAttribute("Value")
        outputUrl = os.path.join(currentFolderUrl, outputUrl)
        outputFolderUrls.append(outputUrl)
    
    return outputType, outputFolderUrls


def _DoReadData(xmlRoot):
    if None == xmlRoot:
        return None
    
    #gets xml data root
    dataRoots = xmlRoot.getElementsByTagName("Data")
    if len(dataRoots) == 0:
        print("there has no data root")
        return None
    
    dataRoot = dataRoots[0]

    #gets data elements
    dataElements = dataRoot.getElementsByTagName("Config")
    if len(dataElements) == 0:
        print("[Error] there has no data")
        return None
    
    #gets current folder url
    currentFolderUrl = os.getcwd()
    
    #gets all of the config datas
    configDatas = []
    for i in range(len(dataElements)):
        dataElement = dataElements[i]
        dataClassify = dataElement.getAttribute("Classify")
        excelName = dataElement.getAttribute("ExcelName")
        excelUrl = os.path.join(currentFolderUrl, "Datas")
        excelUrl = os.path.join(excelUrl, excelName)
        excelSheet = dataElement.getAttribute("ExcelSheet")
        outputName = dataElement.getAttribute("OutputName")
        configData = StartupConfigData.Class(dataClassify, excelUrl, excelSheet, outputName)

        columnElements = dataElement.getElementsByTagName("Data")
        if len(columnElements) == 0:
            print("there has no data type descript")
            break

        for j in range(len(columnElements)):
            columnElement = columnElements[j]
            columnDataName = columnElement.getAttribute("Name")
            columnOutputName = columnElement.getAttribute("Output")
            columnTypeName = columnElement.getAttribute("Type")
            configData.AddColumnData(columnDataName, columnOutputName, columnTypeName)
        configDatas.append(configData)
    
    return configDatas
